package com.dashu.park.controller;

import com.dashu.base.bean.excel.ExcelTeacher;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.school.excel.ExcelFileType;
import com.dashu.base.enums.school.excel.ExcelType;
import com.dashu.base.utils.excel.ExcelUtil;
import com.dashu.park.dto.PageDTO;
import com.dashu.park.dto.company.EnterpriseWorkersDTO;
import com.dashu.park.dto.company.EnterpriseWorkersListDTO;
import com.dashu.park.result.PageEnty;
import com.dashu.park.result.ResultBean;
import com.dashu.park.service.EnterpriseWorkersService;
import com.dashu.park.vo.UpdateEntityVO;
import com.dashu.park.vo.company.EnterpriseWorkersListVO;
import com.dashu.park.vo.company.EnterpriseWorkersVO;
import com.dashu.park.vo.company.UpEnterpriseWorkersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/EnterpriseWorkers")
@RestController
@Slf4j
@Api(value = "dashu/EnterpriseWorkers", tags = {"人员模块"}, description = "后台")
public class EnterpriseWorkersController {

    @Autowired
    private EnterpriseWorkersService enterpriseWorkersService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addTeacher")
    @ApiOperation(value = "人员添加", notes = "人员添加")
    public ResultBean<EnterpriseWorkersDTO> addTeacher(@RequestBody @Validated EnterpriseWorkersVO enterpriseWorkersVO) {
        if (enterpriseWorkersVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员添加", enterpriseWorkersService.add(enterpriseWorkersVO));
    }

//    @PostMapping("/addTeachers")
//    @ApiOperation(value = "批量添加", notes = "批量添加")
//    public ResultBean<List<EnterpriseWorkersDTO>> addPersonals(@RequestParam(value = "pic") MultipartFile file, long companyId) throws IOException {
//        String fileName = file.getOriginalFilename();
//        String fileType = fileName.substring(fileName.lastIndexOf(".")).replace(".", "");
//        if (fileType == null) {
//            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
//        }
//        List<ExcelTeacher> excelTeachers = ExcelUtil.readExcel(file.getInputStream(), ExcelFileType.getExcelFileType(fileType), ExcelType.TEACHER);
//        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "批量添加", enterpriseWorkersService.batchAdd(excelTeachers, companyId));
//    }

    @PostMapping("/updataTeacher")
    @ApiOperation(value = "人员信息修改", notes = "人员信息修改")
    public ResultBean<EnterpriseWorkersDTO> updataTeacher(@RequestBody @Validated UpdateEntityVO<UpEnterpriseWorkersVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员信息修改", enterpriseWorkersService.updataByEnterpriseWorkersId(updateEntityVO));

    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询人员列表", notes = "父节点查询人员列表")
    public ResultBean<List<EnterpriseWorkersDTO>> findBase(@RequestBody @Validated PageEnty<Long> baseVo) {
        if (baseVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询人员列表", enterpriseWorkersService.findByCreteUser(baseVo));

    }

    @GetMapping("/findTeacher")
    @ApiOperation(value = "人员信息查询", notes = "人员信息查询")
    public ResultBean<EnterpriseWorkersDTO> findTeacher(long enterpriseWorkersId) {
        if (enterpriseWorkersId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员信息查询", enterpriseWorkersService.findByEnterpriseWorkersId(enterpriseWorkersId));

    }

    @DeleteMapping("/deleteTeacher")
    @ApiOperation(value = "人员信息删除", notes = "人员信息删除")
    public ResultBean<Boolean> deleteTeacher(long enterpriseWorkersId) {
        if (enterpriseWorkersId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员信息删除", enterpriseWorkersService.deleteByEnterpriseWorkersId(enterpriseWorkersId));

    }
    @PostMapping("/issueBatchTeacher")
    @ApiOperation(value = "人员信息批量下发", notes = "人员信息批量下发")
    public ResultBean<Boolean> issueBatchTeacher(@RequestBody @Validated List<Long> enterpriseWorkersIds) {
        if (enterpriseWorkersIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员信息批量下发", enterpriseWorkersService.issueBatchTeacher(enterpriseWorkersIds));

    }
    @PostMapping("/deleteBatchTeacher")
    @ApiOperation(value = "人员信息批量删除", notes = "人员信息批量删除")
    public ResultBean<Boolean> deleteBatchTeacher(@RequestBody @Validated List<Long> enterpriseWorkersIds) {
        if (enterpriseWorkersIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员信息批量删除", enterpriseWorkersService.deleteBatchTeacher(enterpriseWorkersIds));

    }

    @PostMapping("/pageWorkersList")
    @ApiOperation(value = "人员列表", notes = "人员列表")
    public ResultBean<PageDTO<EnterpriseWorkersListDTO>> pageWorkersList(@RequestBody @Validated PageEnty<EnterpriseWorkersListVO> enterpriseWorkersListVO) throws Exception {
        PageDTO<EnterpriseWorkersListDTO> pageDTO = new PageDTO<>();
        if (enterpriseWorkersListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = enterpriseWorkersService.countWorkersList(enterpriseWorkersListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(enterpriseWorkersService.pageWorkersList(enterpriseWorkersListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "人员列表", pageDTO);

    }
}
